package kr.ac.kaist.vrowser.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import kr.ac.kaist.vrowser.R;
import kr.ac.kaist.vrowser.data.FormElement;
import kr.ac.kaist.vrowser.data.FormSection;

public class FormSectionFragment extends VrowserBaseFragment {
	public FormSection formSection;
	public List<FormElement> elements = new ArrayList<FormElement>();
	
	public RelativeLayout elementLayout;
	public CheckBox checkbox;
	public EditText edittext;
	public EditText password;
	public EditText textarea;
	
	private FormSectionFragment sectionFragment = this;
	FormElementClick elementClick;
	
	public static FormSectionFragment newInstance(FormSection formSection, List<FormElement> elements) {
		FormSectionFragment f = new FormSectionFragment();
		f.elements = elements;
		f.formSection = formSection;
		return f;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_diapad, container, false);
		
		elementLayout = (RelativeLayout) v.findViewById(R.id.elementlayout);
		checkbox = (CheckBox) v.findViewById(R.id.checkbox);
		edittext = (EditText) v.findViewById(R.id.edittext);
		password = (EditText) v.findViewById(R.id.password);
		textarea = (EditText) v.findViewById(R.id.textarea);
		
		try {
			for (int i = 0; i < 6; ++i) {
				if (elements.size() == i) break;
				final FormElement formElement = elements.get(i);
				Button button = (Button) v.findViewById(this.buttons[i]);
				button.setText(cutWords(formElement.name, (i < 5) && (i == elements.size() - 1) && i % 2 == 0));
				button.setContentDescription(String.format(app.getResources().getString(R.string.formSectionButtonDesc), formElement.name));
				button.setBackgroundResource(R.drawable.button_formelement);
				button.setVisibility(View.VISIBLE);
				button.setOnClickListener(new OnClickListener() {
	
					@Override
					public void onClick(View view) {
						
						switch (formElement.type) {
						case Return:
							app.getTextToSpeech().speak(app.getResources().getString(R.string.formElementReturnSpeak), TextToSpeech.QUEUE_FLUSH, null);
							break;
						case Password:
							app.getTextToSpeech().speak(String.format(app.getString(R.string.formElementPasswordSpeak), formElement.name), TextToSpeech.QUEUE_FLUSH, null);
							break;
						case Select:
							app.getTextToSpeech().speak(String.format(app.getString(R.string.formElementPasswordSpeak), formElement.name), TextToSpeech.QUEUE_FLUSH, null);
							app.getTextToSpeech().speak("Select " + formElement.name, TextToSpeech.QUEUE_FLUSH, null);
							break;
						case TextArea:
							app.getTextToSpeech().speak(String.format(app.getString(R.string.formElementTextareaSpeak), formElement.name), TextToSpeech.QUEUE_FLUSH, null);
							break;					
						case Text:
							app.getTextToSpeech().speak(String.format(app.getString(R.string.formElementTextSpeak), formElement.name), TextToSpeech.QUEUE_FLUSH, null);
							break;
						case Submit:
							app.getTextToSpeech().speak(app.getString(R.string.formElementSubmitSpeak), TextToSpeech.QUEUE_FLUSH, null);
							break;
						case CheckBox:
							//app.getTextToSpeech().speak("CheckBox " + formElement.name + " its value is " + formElement.value, TextToSpeech.QUEUE_FLUSH, null);
							break;
						//case Hidden:
						//	break;
						//case Image:
						//	break;
						//case File:
						//	break;
						case Reset:
							app.getTextToSpeech().speak("Reset form", TextToSpeech.QUEUE_FLUSH, null);
							break;
						default:
							break;
						}
						
						if (elementClick != null) {
							elementClick.onFormElementClick(formElement, sectionFragment);
						}
					}
					
				});
			}
		} catch (Exception e) {
		}
		return v;
	}

	
	public void setFormElementClick(FormElementClick elementClick) {
		this.elementClick = elementClick;
	}
	
	public interface FormElementClick {
		public void onFormElementClick(FormElement formElement, FormSectionFragment sectionFragment);
	}
}
